<?php

namespace app\api\controller;

class Index
{
    public function index()
    {
        $countryCode = 'CN'; // 中国的国家代码是CN
        $ipRanges = $this->getCountryIpRanges($countryCode);
        
        if ($ipRanges) {
            echo "该国家的IP段为: " . implode(', ', $ipRanges);
        } else {
            echo "无法获取IP段信息。";
        }
    }


    public function getCountryIpRanges($countryCode)
    {
        $apiUrl = "http://ip-api.com/json/{$countryCode}?fields=range";
        $response = file_get_contents($apiUrl);
        $data = json_decode($response, true);

        if (json_last_error() !== JSON_ERROR_NONE) {
            return false;
        }

        if (isset($data['range'])) {
            return $data['range'];
        }

        return false;
    }
}
